﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class FacilityType:SQLObject
    {
        public FacilityType()
        {}

        public int FacilityTypeID
        { get; set; }

        public string FacilityTypeName
        { get; set; }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateFacilityType");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@facility_type_name", SqlDbType.VarChar, 128).Value = FacilityTypeName;
            command.Parameters.Add("@facility_type_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.FacilityTypeID = (int)command.Parameters["@facility_type_id"].Value;            
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateFacilityType");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@facility_type_name", SqlDbType.VarChar, 128).Value = FacilityTypeName;
            command.Parameters.Add("@facility_type_id", SqlDbType.Int).Value = FacilityTypeID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteFacilityType");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@facility_type_id", SqlDbType.Int).Value = FacilityTypeID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<FacilityType> GetAllFacilityTypes()
        {
            DataTable table = new DataTable();
            List<FacilityType> facilityTypes = new List<FacilityType>();
            SqlCommand command = new SqlCommand("GetFacilityTypes");
            command.CommandType = CommandType.StoredProcedure;           
            
            table = SQLDBLayer.ExecSPTable(command);
           
            foreach (DataRow row in table.Rows)
            {
                FacilityType facilityType = new FacilityType();
                facilityType.FacilityTypeID = (int)row["facility_type_id"];
                facilityType.FacilityTypeName = (string)row["facility_type_name"];
                facilityTypes.Add(facilityType);
            }

            return facilityTypes;
        }
    }
}
